%     VIRTUAL FABRIC - программа для получения эффективных свойств текстильных композитов
%     Copyright (C) 2020  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function Points = Construct_Geometry(V_resin, Fiber_Diameter, Fiber_Packing)

global ErrCode;

fprintf (1, '    Создание базовых точек ячейки периодичности...      ');

switch Fiber_Packing
    
    case 'HEXAGONAL'
        
        Points = zeros(55,2);
        
        d = Fiber_Diameter;
        
        a = d * sqrt(pi() / (2*sqrt(3)*(1 - V_resin)));
        
        if (a <= d)
            fprintf (1, '\nОшибка функции Construct_Geometry: вырожденная геометрия ячейки периодичности!\n');
            fprintf (1, 'В случае Fiber_Packing = HEXAGONAL значение параметра V_resin должно быть больше %f\n', 1 - pi()/(2*sqrt(3)));
            ErrCode = -1;
            return;
        end
        
        Points(1,:) = [0, 0];
        Points(2,:) = [0, d/2 * sqrt(3)/3];
        Points(3,:) = [-d/6, d/2 * sqrt(3)/3];
        Points(4,:) = [-d/3, 0];
        Points(5,:) = [0, d/2];
        Points(6,:) = [-d/4, d/2 * sqrt(3)/2];
        Points(7,:) = [-d/2, 0];
        Points(8,:) = [0, a/2];
        Points(9,:) = [-a/2 * sqrt(3)/3, a/2];
        Points(10,:) = [-a/2 * 2/sqrt(3), 0];
        
        Points(11,:) = [-sqrt(3)/2 * a, 0];
        Points(12,:) = [-sqrt(3)/2 * a + d/2, a/2];
        Points(13,:) = [-sqrt(3)/2 * a + d/4, a/2 - d * sqrt(3)/4];
        Points(14,:) = [-sqrt(3)/2 * a, a/2 - d/2];
        Points(15,:) = [-sqrt(3)/2 * a + d/3, a/2];
        Points(16,:) = [-sqrt(3)/2 * a + d/6, a/2 - sqrt(3)/3 * d/2];
        Points(17,:) = [-sqrt(3)/2 * a, a/2 - sqrt(3)/3 * d/2];
        Points(18,:) = [-sqrt(3)/2 * a, a/2];
        Points(19,:) = [-Points(3,1), Points(3,2)];
        Points(20,:) = [-Points(6,1), Points(6,2)];
        
        Points(21,:) = [-Points(9,1), Points(9,2)];
        Points(22,:) = [-Points(12,1), Points(12,2)];
        Points(23,:) = [-Points(15,1), Points(15,2)];
        Points(24,:) = [-Points(18,1), Points(18,2)];
        Points(25,:) = [-Points(4,1), Points(4,2)];
        Points(26,:) = [-Points(7,1), Points(7,2)];
        Points(27,:) = [-Points(10,1), Points(10,2)];
        Points(28,:) = [-Points(13,1), Points(13,2)];
        Points(29,:) = [-Points(16,1), Points(16,2)];
        Points(30,:) = [-Points(17,1), Points(17,2)];
        
        Points(31,:) = [-Points(14,1), Points(14,2)];
        Points(32,:) = [-Points(11,1), Points(11,2)];
        Points(33,:) = [Points(2,1), -Points(2,2)];
        Points(34,:) = [Points(5,1), -Points(5,2)];
        Points(35,:) = [Points(8,1), -Points(8,2)];
        Points(36,:) = [Points(19,1), -Points(19,2)];
        Points(37,:) = [Points(20,1), -Points(20,2)];
        Points(38,:) = [Points(21,1), -Points(21,2)];
        Points(39,:) = [Points(31,1), -Points(31,2)];
        Points(40,:) = [Points(28,1), -Points(28,2)];
        
        Points(41,:) = [Points(22,1), -Points(22,2)];
        Points(42,:) = [Points(30,1), -Points(30,2)];
        Points(43,:) = [Points(29,1), -Points(29,2)];
        Points(44,:) = [Points(23,1), -Points(23,2)];
        Points(45,:) = [Points(24,1), -Points(24,2)];
        Points(46,:) = [Points(3,1), -Points(3,2)];
        Points(47,:) = [Points(6,1), -Points(6,2)];
        Points(48,:) = [Points(9,1), -Points(9,2)];
        Points(49,:) = [Points(14,1), -Points(14,2)];
        Points(50,:) = [Points(13,1), -Points(13,2)];
        
        Points(51,:) = [Points(12,1), -Points(12,2)];
        Points(52,:) = [Points(17,1), -Points(17,2)];
        Points(53,:) = [Points(16,1), -Points(16,2)];
        Points(54,:) = [Points(15,1), -Points(15,2)];
        Points(55,:) = [Points(18,1), -Points(18,2)];
        
    case 'SQUARE'
        
        Points = zeros(33,2);
        
        d = Fiber_Diameter;
        
        a = d/2 * sqrt(pi() / (1 - V_resin));
        
        if (a <= d)
            fprintf (1, '\nОшибка функции Construct_Geometry: вырожденная геометрия ячейки периодичности!\n');
            fprintf (1, 'В случае Fiber_Packing = SQUARE значение параметра V_resin должно быть больше %f\n', 1 - pi()/4);
            ErrCode = -1;
            return;
        end
        
        Points(1,:) = [0, 0];
        Points(2,:) = [0, a/2];
        Points(3,:) = [-a/2, 0];
        Points(4,:) = [-a/2 + d/2, a/2];
        Points(5,:) = [-a/2 + d/2 * sqrt(2)/2, a/2 - d/2 * sqrt(2)/2];
        Points(6,:) = [-a/2, a/2 - d/2];
        Points(7,:) = [-a/2 + 2/3 * d/2, a/2];
        Points(8,:) = [-a/2 + 2/3 * d/2 * sqrt(2)/2, a/2 - 2/3 * d/2 * sqrt(2)/2];
        Points(9,:) = [-a/2, a/2 - 2/3 * d/2];
        Points(10,:) = [-a/2, a/2];
        
        Points(11,:) = [-Points(4,1), Points(4,2)];
        Points(12,:) = [-Points(5,1), Points(5,2)];
        Points(13,:) = [-Points(6,1), Points(6,2)];
        Points(14,:) = [-Points(7,1), Points(7,2)];
        Points(15,:) = [-Points(8,1), Points(8,2)];
        Points(16,:) = [-Points(9,1), Points(9,2)];
        Points(17,:) = [-Points(10,1), Points(10,2)];
        Points(18,:) = [-Points(3,1), Points(3,2)];
        Points(19,:) = [Points(13,1), -Points(13,2)];
        Points(20,:) = [Points(16,1), -Points(16,2)];
        
        Points(21,:) = [Points(17,1), -Points(17,2)];
        Points(22,:) = [Points(12,1), -Points(12,2)];
        Points(23,:) = [Points(15,1), -Points(15,2)];
        Points(24,:) = [Points(11,1), -Points(11,2)];
        Points(25,:) = [Points(14,1), -Points(14,2)];
        Points(26,:) = [Points(2,1), -Points(2,2)];
        Points(27,:) = [Points(4,1), -Points(4,2)];
        Points(28,:) = [Points(7,1), -Points(7,2)];
        Points(29,:) = [Points(10,1), -Points(10,2)];
        Points(30,:) = [Points(5,1), -Points(5,2)];
        
        Points(31,:) = [Points(8,1), -Points(8,2)];
        Points(32,:) = [Points(6,1), -Points(6,2)];
        Points(33,:) = [Points(9,1), -Points(9,2)];
        
    otherwise
        
        fprintf (1, '\nОшибка функции Construct_Geometry: недопустимое значение параметра Fiber_Packing !\n');
        ErrCode = -1;
        return;
    
end

fprintf (1, 'OK!\n');
